package com.glodon.paas.document.storage.util;

import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();
    static {
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    @SuppressWarnings("rawtypes")
    public static Map jsonToMap(String json) throws IOException {
        return objectMapper.readValue(json, Map.class);
    }

    public static String mapToJson(Map<?, ?> params) throws IOException {
        return objectMapper.writeValueAsString(params);
    }

    public static <T> T jsonToObject(String json, Class<T> type) throws IOException {
        return objectMapper.readValue(json, type);
    }

    public static String objectToJson(Object object) throws IOException {
        return objectMapper.writeValueAsString(object);
    }
}
